//
//  HTTripDeatilTableViewCell.m
//  HeartTravel
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTTripDeatilTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "HTTripDeattillCommentPhotoModel.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"

#define W (SCREEN_WIDTH - 60 - 10) / 3//图片的宽高

@implementation HTTripDeatilTableViewCell

- (void)setCommentMD:(HTTripDeattillCommentModel *)commentMD
{
    CGFloat startY = [HTTripDeatilTableViewCell heigtForString:commentMD.content];
    
    _commentMD = commentMD;
    
    _autherName.text = commentMD.user_name;
    
    _time.text = commentMD.create_date;
    
    _content.text = commentMD.content;
    _content.frame = CGRectMake(22, 65, 347, [HTTripDeatilTableViewCell heigtForString:commentMD.content]);
    _content.font = [UIFont systemFontOfSize:14];
    _content.numberOfLines = 0;
    
    _likeNumber.text = commentMD.favorite_cnt;
    
    _commentNumber.text = commentMD.comment_cnt;
    
    [_autherPicture sd_setImageWithURL:[NSURL URLWithString:commentMD.avatar]];
    _autherPicture.tag = 1000;
    _autherPicture.layer.cornerRadius = 20;
    _autherPicture.layer.masksToBounds = YES;
    

    //防止循环引用
    for (UIView * view in self.contentView.subviews) {
        
        if ([view isMemberOfClass:[UIImageView class]] && view.tag != 1000 && view.tag != 500) {
            
            [view removeFromSuperview];
        }
    }
    
    
    if (commentMD.status_photoArr.count > 0) {
        
        if (commentMD.content.length == 0) {
            
            for (int i = 0; i <= commentMD.status_photoArr.count - 1; i++) {
                
                HTTripDeattillCommentPhotoModel *photoMD = [[HTTripDeattillCommentPhotoModel alloc]init];
                photoMD = commentMD.status_photoArr[i];

                int row = i/3;
                int column = i%3;
                
                UIImageView *picture = [[UIImageView alloc]initWithFrame:CGRectMake(30+column * (W + 5), 65  + row * (W + 5), W, W)];
                picture.tag = i;
                picture.userInteractionEnabled = YES;
                [picture sd_setImageWithURL:[NSURL URLWithString:photoMD.thumb_img_url]];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage:)];
                [picture addGestureRecognizer:tap];
                [self.contentView addSubview:picture];
          
           }
            
    }else{
            for (int i = 0; i <= commentMD.status_photoArr.count - 1; i++) {
                
                HTTripDeattillCommentPhotoModel *photoMD = [[HTTripDeattillCommentPhotoModel alloc]init];
                photoMD = commentMD.status_photoArr[i];
                
                int row = i/3;
                int column = i%3;
                
                UIImageView *picture = [[UIImageView alloc]initWithFrame:CGRectMake(30+column * (W + 5), 65 + 15 + startY + row * (W + 5), W, W)];
                picture.tag = i;
                [picture sd_setImageWithURL:[NSURL URLWithString:photoMD.thumb_img_url]];
                picture.userInteractionEnabled = YES;
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage:)];
                [picture addGestureRecognizer:tap];
                
                [self.contentView addSubview:picture];
                
              }

        }
        
    }
   
}

- (void)tapImage:(UITapGestureRecognizer *)tap
{
    NSInteger count = _commentMD.status_photoArr.count;
    
    //1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
    
    for (int i = 0 ; i < count; i ++) {
        
        HTTripDeattillCommentPhotoModel *model = [[HTTripDeattillCommentPhotoModel alloc]init];
        model = _commentMD.status_photoArr[i];
        
        MJPhoto *photo = [[MJPhoto alloc]init];
        
        NSString *imageUrlStr = [self normolWithString:model.img_url];
        
        photo.url = [NSURL URLWithString:imageUrlStr];
        
        if ([[self.contentView viewWithTag: i] isKindOfClass:[UIImageView class]]) {
            
            photo.srcImageView = (UIImageView *)[self.contentView viewWithTag: i];
        }
        
        [photos addObject:photo];
        
    }
    
    //2.显示放大的图片
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    //这里是imageveiw的个数,注意tag值的添加与数组的个数相一致,不然会出现数组越界的问题
    browser.currentPhotoIndex = tap.view.tag;//弹出相册时显示
    //注意这里是装imageview的数组
    browser.photos = photos;//设置所有的图片
    
    [browser show];
    
}

//判断字符串是否为空
- (id) normolWithString:(id)string
{
    if ([string isKindOfClass:[NSNull class]] || string == nil) {
        return @"";
    }
    return string;
}


+ (CGFloat)heigtForString:(NSString *)str
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    CGRect bounds = [str boundingRectWithSize:CGSizeMake(347, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return bounds.size.height;
 
}



+(CGFloat)heightForComment:(HTTripDeattillCommentModel *)commentModel
{
    if (commentModel.status_photoArr.count >=4 && commentModel.status_photoArr.count <=6 && commentModel.content.length > 0) {
        
        return 17 + 40 + [self heigtForString:commentModel.content] + W * 2 + 5 + 58;
        
    }else if (commentModel.status_photoArr.count > 0 && commentModel.status_photoArr.count <= 3 && commentModel.content.length > 0)
    {
        
        return 17 + 40 + [self heigtForString:commentModel.content] + W  + 5 + 58;
        
        
    } else if (commentModel.status_photoArr.count > 0 && commentModel.status_photoArr.count <= 3)
    {
        return 17 + 40 + [self heigtForString:commentModel.content] + W  + 38 ;
        
    }else
    {
        return 17 + 40 + [self heigtForString:commentModel.content]  + 48 ;
    }
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
